package site.wanjiahao.handler.client;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.Promise;
import site.wanjiahao.factory.ProxyFactory;
import site.wanjiahao.message.response.RpcResponseMessage;

@ChannelHandler.Sharable
public class RpcResponseMessageHandler extends SimpleChannelInboundHandler<RpcResponseMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponseMessage msg) throws Exception {
        Promise<Object> promise = ProxyFactory.getPromise(msg.getRequestId());
        if (msg.isSuccess()) {
            promise.setSuccess(msg.getResult());
        } else {
            promise.setFailure(new RuntimeException(msg.getReason()));
        }
    }
}
